"""
Pydantic 版本（目前只有 V1 和 V2）
"""

from pydantic.version import VERSION as PYDANTIC_VERSION


PYDANTIC_V1: bool = PYDANTIC_VERSION.startswith("1")
""" 是否是 Pydantic V1 版本 """

PYDANTIC_V2: bool = PYDANTIC_VERSION.startswith("2")
""" 是否是 Pydantic V2 版本 """

if PYDANTIC_V1:
    from pydantic import (
        BaseModel as V1BaseModel,
        ValidationError as V1ValidationError,
    )

    V2BaseModel = None
    V2ValidationError = None

elif PYDANTIC_V2:
    from pydantic.v1 import (
        BaseModel as V1BaseModel,
        ValidationError as V1ValidationError,
    )
    from pydantic import (
        BaseModel as V2BaseModel,
        ValidationError as V2ValidationError,
    )

else:
    raise RuntimeError(f"Unsupported Pydantic version: {PYDANTIC_VERSION}")


def is_v1_model(model) -> bool:
    """ 是否是 Pydantic V1 模型 """

    return (
        issubclass(model, V1BaseModel)
        or isinstance(model, V1BaseModel)
    )


def is_v2_model(model) -> bool:
    """ 是否是 Pydantic V2 模型 """

    if not PYDANTIC_V2:
        return False

    return (
        issubclass(model, V2BaseModel)
        or isinstance(model, V2BaseModel)
    )


def is_v1_error(error) -> bool:
    """ 是否是 Pydantic V1 的 ValidationError """

    return (
        issubclass(error, V1ValidationError)
        or isinstance(error, V1ValidationError)
    )


def is_v2_error(error) -> bool:
    """ 是否是 Pydantic V1 的 ValidationError """

    if not PYDANTIC_V2:
        return False

    return (
        issubclass(error, V2ValidationError)
        or isinstance(error, V2ValidationError)
    )
